﻿using System;
namespace Factory; 

class Program
{//形状抽象类（父类）
    abstract class Shape
    {
        public abstract double getArea();         //面积     
    }
 
    class Rectangle : Shape
    {
        double wid, len;
        public Rectangle(double widx, double lenx)
        {
            this.wid = widx;
            this.len = lenx;
        }
        public override double getArea()
        {
            return wid * len;
        }
    }

    class Square : Shape
    {
        double side;
        public Square(double sidex)
        {
            this.side = sidex;
        }
        public override double getArea()
        {
            return side * side;
        }
    }

    class Triangle : Shape
    {
        double a, b, c;

        public Triangle(double ax, double bx, double cx)
        {
            this.a = ax;
            this.b = bx;
            this.c = cx;
        }

        public override double getArea()
        {
            double m = (a + b + c) / 2;
            return  Math.Sqrt(m * (m-a) * (m-b) * (m-c));
        }
    }

    class RandomFactory
    {
        public Shape createShape()
        {
            Shape shape = null;
            Random random;
            random = new Random();

            int i;
            i = random.Next(0, 3);

            if (i == 0)
            {
                shape = new Rectangle(random.Next(1, 10),random.Next(1,10));
            }

            else if (i == 1)
            {
                shape = new Square(random.Next(1, 10));
            }

            else if(i == 2)
            {
                shape = new Triangle(random.Next(1, 10),random.Next(1,10),random.Next(1,10));
            }
            return shape;
        }
    }

    static void Main(string[] args)
    {
        RandomFactory ot = new RandomFactory();
        Shape[] s = new Shape[10];
        double sum = 0;
        for (int i = 0; i < 10; i++)
        {
            s[i] = ot.createShape();
        }
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("创建的第{0}图形是", i + 1);
            if (s[i] is Rectangle) Console.WriteLine("长方形");
            else if (s[i] is Square) Console.WriteLine("正方形");
            else if (s[i] is Triangle) Console.WriteLine("三角形");
            Console.WriteLine("面积为：" + String.Format("{0}", s[i].getArea()));
        }
        for (int i = 0; i < 10; i++)
        {
            sum += s[i].getArea();
        }
        Console.WriteLine("面积之和为{0}：", sum);
    }
}